package org.hdlh.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.hdlh.core.RequestHolder;
import org.hdlh.core.RpcFuture;
import org.hdlh.core.RpcProtocol;
import org.hdlh.core.RpcResponse;
@Slf4j
public class RpcClientHandler extends SimpleChannelInboundHandler<RpcProtocol<RpcResponse>> {


    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, RpcProtocol<RpcResponse> rpcResponseRpcProtocol) throws Exception {
        log.info("receive rpc server result");
        long requestId = rpcResponseRpcProtocol.getHeader().getRequestId();
        RpcFuture<RpcResponse> future = RequestHolder.REQUEST_MAP.remove(requestId);
        //返回结果
        future.getPromise().setSuccess(rpcResponseRpcProtocol.getContent());
    }
}
